<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');
require_once('../../_inc/funct.getLists.php');
$aLists = getLists();
$aListH = array(''=>'-- seleziona una lista --');
foreach($aLists as $cX => $aX) {
	$aListH[$cX] = $aX['descr'];
}
if (reqVar('azione')=='elimina') {
	$sSql = "DELETE FROM ovf_campi_list WHERE cod_list = '" .reqVarAS('cod')."'".
			"AND tipo_list = '" .reqVarAS('tipo')."'";
	$objDB->exec($sSql);
	header("Location: ".urlTorna("lista eliminata"));
	exit();
}
reqDef('cod_list', reqVar('cod'));
$sqlSel = "SELECT * FROM ovf_campi_list " .
			" WHERE tipo_list = '" .reqVarAS('tipo')."' ".
			" AND cod_list = '".reqVarAS('cod')."' ".
			" ORDER BY seq ";
$objDB->exec($sqlSel);
$k=1;
while($aRow = $objDB->fetch()) {
	reqDef("campo_seq_$k", $aRow['seq']);
	reqDef("campo_label_$k", $aRow['label']);
	if (substr($aRow['campo'],0,2)=='W_') {
		reqDef("campo_calc_$k", substr($aRow['campo'],2));
		reqDef("campo_$k", '=calc=');
	} else {
		reqDef("campo_$k", $aRow['campo']);
	}
	reqDef("campo_size_$k", $aRow['campo_size']);
	reqDef("campo_type_$k", $aRow['campo_type']);
	reqDef("campo_dec_$k", $aRow['campo_dec']);
	reqDef("campo_style_$k", $aRow['campo_style']);
	reqDef("campo_align_$k", $aRow['campo_align']);
	$k++;
}
$aTypes['stringa'] = 'stringa';
$aTypes['numero'] = 'numero';
$aTypes['data'] = 'data';
$aTypes['bool'] = 'Si\'/No';

if (reqVar('azione') == 'salva') {
	$msgPag = conferma();
} 
$sCmdR ='';
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:submAz('salva');\" >salva</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:confSubmAz('elimina');\" >elimina</a>";
$sCmdR .= cmdTorna('torna');

$aStili[''] = 'normale';
$aStili['b'] = 'grassetto';
$aStili['i'] = 'corsivo';
$aStili['ib'] = 'gras/cors';

$aAll['sinistra'] = 'sinistra';
$aAll['destra'] = 'destra';
$aAll['centro'] = 'centro';

$aCampi = array(''=>'-- seleziona un campo --');
foreach($aLists[reqvar('tipo')]['files'] as $nomeX => $aX) {
	$aCampiDef = getDefCampi($nomeX);
	foreach($aCampiDef as $campoY => $aY) {
		$aCampoZ = $aX['alias']."_".$campoY;
		$aCampi[$aCampoZ] = $aCampoZ;
	}
}
$aCampi['=calc='] = _t('= calcolato =');
testaHtml('Gestione liste', 'Nuova lista',$msgPag, 'gest');
echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('tipo');
echo getHtmlHidden('cod');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
function confSubmAz(az) {
	if (confirm("Confermi ?")) {
		document.frm1.azione.value = az;
		document.frm1.submit();
	}
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
 			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php echo getHtmlRigaRif(20);?>
   <tr>
      <td class="lab1" colspan='2' align='left'>Tipo lista</td>
      <td class="txt1" colspan='18' align='left'>
			<?php echo reqVar('tipo')." - ".ovfVx($aLists, reqVar('tipo'), 'descr');?>
		</td>
	</tr>
	<tr>
      <td class="lab1" colspan='2' align='left'>Cod. lista</td>
      <td class="txt1" colspan='4' align='left'>
			<?php echo reqVar('cod'); ?>
		</td>
      <td class="lab1" colspan='2' align='left'>Nuovo codice</td>
      <td class="txt1" colspan='12' align='left'>
			<?php echo getHtmlText('cod_list', 30,30); ?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='2'>File</td>
      <td class="txt1" colspan='18' align='left'>
      	<?
      	if (isset($aLists[reqvar('tipo')]['files']))
				foreach($aLists[reqvar('tipo')]['files'] as $nomeX => $aX) {
					echo $aX['alias']." - ".$nomeX."<br/>";
				}	
			?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='1' align='center'>N.</td>
      <td class="lab1" colspan='1' align='center'>Seq.</td>
      <td class="lab1" colspan='4' align='center'>Etichetta</td>
      <td class="lab1" colspan='4' align='center'>Campo<br/>Nome calc.</td>
      <td class="lab1" colspan='2' align='center'>Largh.</td>
      <td class="lab1" colspan='2' align='center'>Tipo</td>
      <td class="lab1" colspan='1' align='center'>Dec.</td>
      <td class="lab1" colspan='5' align='center'>Stile</td>
   </tr>
<?php 
if (!empty($aCampi)) for($k=1;$k<11;$k++) {
?>
   <tr>
      <td class="txt1" colspan='1' align='right' valign='top'>
			<?=$k;?>)&nbsp;
		</td>
      <td class="txt1" colspan='1' align='center' valign='top'>
			<?=getHtmlText("campo_seq_$k",1);?>
		</td>
      <td class="txt1" colspan='4' align='center' valign='top'>
			<?=getHtmlText("campo_label_$k",30);?>
		</td>
      <td class="txt1" colspan='4' align='left' valign='top'>
			<?
			echo getHtmlSelect("campo_$k",$aCampi);
			echo getHtmlText("campo_calc_$k",25);
			?>
		</td>
      <td class="txt1" colspan='2' align='center' valign='top'>
			<?=getHtmlText("campo_size_$k",1);?>
		</td>
      <td class="txt1" colspan='2' align='center' valign='top'>
			<?=getHtmlSelect("campo_type_$k",$aTypes);?>
		</td>
      <td class="txt1" colspan='1' align='center' valign='top'>
			<?=getHtmlTextNum("campo_dec_$k",1);?>
		</td>
      <td class="txt1" colspan='5' align='center' valign='top'>
			<?
			echo getHtmlSelect("campo_style_$k",$aStili);
			echo getHtmlSelect("campo_align_$k",$aAll);?>
		</td>
   </tr>
<?php 
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
 			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
			<?
echo "<form/>\n";
codaHtml();
exit();
function conferma() {
	global $objDB;
	$msg = '';
	
	$_REQUEST['cod_list'] = strtolower(trim(reqVar('cod_list')));
	if (reqVar('cod_list')=='') {
		$msg .= "digita un codice lista<br>";
	}
	if (preg_match("/[^-a-z0-9_.() ]/",reqVar('cod_list'))) {
		$msg .= "codice lista - ammessi solo lettere, numeri e - _ . ( )<br>";
	}
	$aSeq = array();
	$totSlot = 0;
	for($k=1;$k<11;$k++) {
		if (reqVar("campo_seq_$k")=='' && reqVar("campo_$k")=='' 
		&& reqVar("campo_label_$k")=='' && reqVar("campo_size_$k")=='') {
			continue;
		}
		if (reqVarStr2num("campo_size_$k")===false || reqVarStr2num("campo_size_$k")==0) {
			$msg .= "larghezza $k errata<br>";
		}
		$totSlot += reqVarInt("campo_size_$k");
		if (reqVarStr2num("campo_seq_$k")===false || reqVarStr2num("campo_seq_$k")==0) {
			$msg .= "seq. $k errata<br>";
		}
		if (reqVar("campo_$k")=='') {
			$msg .= "manca campo $k<br>";
		}
		if (in_array(reqVarInt("campo_seq_$k"),$aSeq)) {
			$msg .= "seq. $k doppia<br>";
		} else {
			$aSeq[] = reqVarInt("campo_seq_$k");
		}
		if (reqVarStr2num("campo_dec_$k")===false) {
			$msg .= "decimali $k errati<br>";
		}
		$_REQUEST["campo_calc_$k"] = strtolower(reqVarTrim("campo_calc_$k"));
		if (preg_match("/[^a-z0-9_]/",reqVarTrim("campo_calc_$k"))) {
			$msg .= "campo $k calcolato - nome errato (lettere, numeri e _)<br>";
		}
		if (reqVar("campo_$k")=='=calc=' && reqVarTrim("campo_calc_$k")=='') {
			$msg .= "campo $k calcolato - manca nome<br>";
		}
		if (reqVar("campo_$k")!='=calc=' && reqVarTrim("campo_calc_$k")!='') {
			$msg .= "campo $k non calcolato - no nome<br>";
		}
	}
	if ($totSlot!=20) {
		$msg .= "somma larghezze deve essere 20<br>";
	}
	if (reqVar('cod_list')!=reqVar('cod')) {
		$sqlSel = "SELECT * FROM ovf_campi_list " .
				" WHERE tipo_list = '" .reqVarAS('tipo')."' ".
				" AND cod_list = '".reqVarAS('cod_list')."' ";
				" AND cod_list <> '".reqVarAS('cod')."' ";
		$objDB->exec($sqlSel);
		$aRow = $objDB->fetch();
		if ($aRow) {
			$msg .= "codice lista gia' esistente<br>";
		}
	}
	if ($msg != '') {
		return $msg;
	}
	
	$sSql = "DELETE FROM ovf_campi_list WHERE cod_list = '" .reqVarAS('cod')."'".
			"AND tipo_list = '" .reqVarAS('tipo')."'";
	//dbg_($sSql);
	$objDB->exec($sSql);
	for($k=1;$k<11;$k++) {
		if (reqVarInt("campo_seq_$k") == 0) continue;
		$campo = (reqVar("campo_$k")=='=calc=')?"W_".reqVarAS("campo_calc_$k"):reqVarAS("campo_$k");
		$sSql = "INSERT INTO ovf_campi_list SET cod_list = '" .reqVarAS('cod_list')."'".
				", tipo_list = '" .reqVarAS('tipo')."'".
				", seq = ".reqVarInt("campo_seq_$k").
				", label = '".reqVarAS("campo_label_$k")."'".
				", campo = '".$campo."'".
				", campo_size = ".reqVarInt("campo_size_$k").
				", campo_type = '".reqVarAS("campo_type_$k")."' ".
				", campo_dec = ".reqVarInt("campo_dec_$k").
				", campo_style = '".reqVarAS("campo_style_$k")."' ".
				", campo_align = '".reqVarAS("campo_align_$k")."' ";
		//dbg_($sSql);
		$objDB->exec($sSql);
	}
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: ".urlTorna("lista inserita"));
	exit();
}
?>